package com.upup.train_management.service.impl;

import com.upup.train_management.entity.Schedule;
import com.upup.train_management.entity.Train;
import com.upup.train_management.repository.ScheduleRepository;
import com.upup.train_management.repository.TicketRepository;
import com.upup.train_management.repository.TrainRepository;
import com.upup.train_management.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    private final ScheduleRepository scheduleRepository;
    private final TrainRepository trainRepository;
    private final TicketRepository ticketRepository;

    @Autowired
    public ScheduleServiceImpl(
            ScheduleRepository scheduleRepository,
            TrainRepository trainRepository,
            TicketRepository ticketRepository) {
        this.scheduleRepository = scheduleRepository;
        this.trainRepository = trainRepository;
        this.ticketRepository = ticketRepository;
    }

    @Override
    public List<Schedule> getAllSchedules() {
        return scheduleRepository.findAll();
    }

    @Override
    public Schedule getScheduleById(Long id) {
        return scheduleRepository.findById(id).orElse(null);
    }

    @Override
    public Schedule saveSchedule(Schedule schedule) {
        return scheduleRepository.save(schedule);
    }

    @Override
    public void deleteSchedule(Long id) {
        scheduleRepository.deleteById(id);
    }

    @Override
    public List<Schedule> findSchedulesByTrainId(Long trainId) {
        return scheduleRepository.findByTrainId(trainId);
    }

    @Override
    public List<Schedule> findSchedulesByRouteId(Long routeId) {
        return scheduleRepository.findByRouteId(routeId);
    }

    @Override
    public List<Schedule> findSchedulesByDate(LocalDate date) {
        return scheduleRepository.findByDepartureDate(date);
    }

    @Override
    public List<Schedule> findSchedulesByDateRange(LocalDate startDate, LocalDate endDate) {
        return scheduleRepository.findByDepartureDateBetween(startDate, endDate);
    }

    @Override
    public List<Schedule> searchSchedules(Long startStationId, Long endStationId, LocalDate date) {
        return scheduleRepository.findSchedulesByStationsAndDate(startStationId, endStationId, date);
    }

    @Override
    public List<Schedule> getActiveSchedules() {
        LocalDateTime now = LocalDateTime.now();
        return scheduleRepository.findByDepartureDateTimeAfter(now.toLocalDate(), now.toLocalTime());
    }

    @Override
    public int getAvailableSeats(Long scheduleId) {
        Schedule schedule = scheduleRepository.findById(scheduleId).orElse(null);
        if (schedule == null) {
            return 0;
        }

        // 获取列车总座位数
        Train train = trainRepository.findById(schedule.getTrainId()).orElse(null);
        if (train == null) {
            return 0;
        }

        // 计算已售票数
        int soldTickets = ticketRepository.countByScheduleIdAndStatusNot(scheduleId, "CANCELLED");

        // 返回可用座位数
        return train.getTotalSeats() - soldTickets;
    }

    @Override
    public boolean updateScheduleStatus(Long scheduleId, String status) {
        Schedule schedule = scheduleRepository.findById(scheduleId).orElse(null);
        if (schedule == null) {
            return false;
        }
        schedule.setStatus(status);
        scheduleRepository.save(schedule);
        return true;
    }
}